<?php

namespace lib;

use lib\Server\Server;

class Root
{
	static private $config       = null;
	static private $_config_file = ROOT_DIR . '/server.yaml';

	public function getConfig($config_node=null, $config_key = false)
	{
		if (!file_exists(self::$_config_file)) {
			throw new ServerException('Message:Config file [' . self::$_config_file . '] not exists');
		}
		else {
			if (!self::$config) {
				self::$config = \Spyc::YAMLLoad(self::$_config_file);
			}

			if(!$config_node){
				return self::$config;
			}

			if (!$config_key) {
				if (!is_array(self::$config[$config_node])) return [];

				$config = [];
				foreach (self::$config[$config_node] as $key => $value) $config[$key] = $value;

				return $config;
			}
			else {
				return self::$config[$config_node][$config_key] ?: null;
			}

		}
	}


	static public function Config($config_node=null, $config_key = false)
	{
		return (new self())->getConfig($config_node, $config_key);
	}

	static public function stdout($message){
		echo $message.PHP_EOL;
	}
}